<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();

$action = $_POST['action'];

try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'submit':
      submit($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo ERROR_SIGN . $e->getMessage();
}


function submit($myPdo) {
  if (!isset($_SESSION[ADMIN])) {
    return;
  }
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  
  $cond_vals = new stdClass();
  $cond_vals->c = 'instance_id = :v1';
  $cond_vals->v = array(':v1' => $q->instance_id);
  $result = DbUtils::get(DbUtils::createPdoInst(), TABLE_NAME_PREFIX_PLUGIN . 'user_page_manage', $cond_vals, NULL, NULL, NULL, NULL, NULL);
  if (count($result->d) == 1) {
    $result->d[0]->system_id = $q->system_id;
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'page_manage', $result->d[0]);
  } else {
    $tmp = new stdClass();
    $tmp->instance_id = $q->instance_id;
    $tmp->system_id = $q->system_id;
    DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'page_manage', $tmp);
  }
  
  echo json_encode(array());
}
?>